﻿using System.Collections.Generic;
using System.Linq;

namespace Tuxedo.Compile
{
	internal class IfStatement : CodeLine
	{
		private Expression condition;
		private CodeLine[] trueCode;
		private CodeLine[] falseCode;

		public IfStatement(Expression condition, IList<CodeLine> trueCode, IList<CodeLine> falseCode)
		{
			this.condition = condition;
			this.trueCode = trueCode.ToArray();
			this.falseCode = falseCode.ToArray();
		}

		public static CodeLine Parse(TokenStream tokens)
		{
			if (tokens.Peek().LineValue.Contains("if (Math.Abs(dy) < 6) this.mode = \"right\";"))
			{

			}
			tokens.PopExpected("if");
			tokens.PopExpected("(");
			Expression condition = Expression.ParseExpression(tokens);
			tokens.PopExpected(")");

			IList<CodeLine> trueStatements = CodeLine.ParseBlock(tokens, true);
			IList<CodeLine> falseStatements = CodeLine.NO_LINES;
			if (tokens.PopIfPresent("else"))
			{
				falseStatements = CodeLine.ParseBlock(tokens, true);
			}

			return new IfStatement(condition, trueStatements, falseStatements);
		}
	}
}
